package com.xhj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhj.entity.Member;
import com.xhj.mapper.MemberMapper;
import com.xhj.service.MemberService;
import com.xhj.util.JwtUtil;
import com.xhj.vo.LoginVo;
import com.xhj.vo.RegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


/**
 * 会员表(UcenterMember)表服务实现类
 *
 * @author makejava
 * @since 2021-03-08 14:42:27
 */
@Service()
public class MemberServiceImpl extends ServiceImpl<MemberMapper,Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        Member memberGetByMobile = this.getByMobile(mobile);
        if (memberGetByMobile != null) throw new RuntimeException("该手机号已经注册！");
        // 假如手机号未被注册 获取用户填的验证码
        String code = registerVo.getCode();
        String codeInRedis = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isEmpty(codeInRedis)) throw new RuntimeException("验证码已经过期！请重新发送");
        if (!codeInRedis.equals(code)) throw new RuntimeException("验证码不正确！");
        Member member = new Member();
        member.setMobile(mobile);
        member.setNickname(registerVo.getNickname());
        // 使用盐值加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        member.setPassword(passwordEncoder.encode(registerVo.getPassword()));
        member.setIsDisabled(0);
        member.setAvatar("http://bossxie.oss-cn-guangzhou.aliyuncs.com/2021二月05/ecd33feaef0c455a833375a280a742d3.jpg");
        this.save(member);
    }

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        if (StringUtils.isEmpty(mobile)) throw new RuntimeException("手机号码不能为空！");
        Member memberGetByMobile = this.getByMobile(mobile);
        if (memberGetByMobile == null) throw new RuntimeException("手机号码错误！");
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        // 解密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(password)) throw new RuntimeException("密码不能为空！");
        // 匹配密码
        boolean matches = passwordEncoder.matches(password, memberGetByMobile.getPassword());
        if (!matches) throw new RuntimeException("密码不正确！");
        // 校验是否被禁用
        if (memberGetByMobile.getIsDisabled() == 1) throw new RuntimeException("该用户已经被禁用！");
        String token = JwtUtil.getJwtToken(memberGetByMobile.getId(), memberGetByMobile.getNickname());
        return token;
    }

    @Override
    public Member getByMobile(String mobile) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        return this.getOne(queryWrapper);
    }

    @Override
    public LoginVo getLoginInfo(String memberId) {
        Member member = this.getById(memberId);
        LoginVo loginVo = new LoginVo();
        BeanUtils.copyProperties(member,loginVo);
        loginVo.setPassword("");
        return loginVo;
    }

    @Override
    public Member getByOpenId(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        return this.getOne(queryWrapper);
    }

    @Override
    public int countRegister(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}